<template>
  <div class="dialog-container">
    <el-dialog
      v-model="dialogVisible"
      :before-close="handleClose"
      :title="title"
      width="40%"
    >
      <div v-if="dialogtype === 'code' || dialogtype === 'power'">
        <template v-for="(item, index) in diologcslist" :key="index">
          <el-tag v-if="item.Code" class="tag" effect="plain">
            {{ item.Code }}
          </el-tag>
        </template>
      </div>
      <el-form label-position="left" label-width="125px">
        <el-form-item
          v-for="(field, index) in dialogFields"
          :key="index"
          :label="field.label"
        >
          <template v-if="field.type === 'input'">
            <el-input
              v-model="field.model"
              :disabled="field.disabled || false"
              style="width: 350px"
            />
          </template>
          <template v-else-if="field.type === 'inputnumber'">
            <el-input-number
              v-model="field.model"
              label="屏幕亮度"
              :max="220"
              :min="0"
            />
          </template>
          <template v-else-if="field.type === 'radio'">
            <el-radio-group v-model="field.model" class="ml-4">
              <el-radio
                v-for="radio in field.group"
                :key="radio.label"
                :label="radio.label"
              >
                {{ radio.text }}
              </el-radio>
            </el-radio-group>
          </template>
          <template v-else-if="field.type === 'select'">
            <el-select v-model="field.model" placeholder="select">
              <el-option
                v-for="item in field.options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </template>
          <template v-else-if="field.type === 'switch'">
            <el-switch v-model="field.model" />
          </template>
          <template v-else-if="dialogFields.type === 'bindImg'">
            <el-input
              v-model="field.model"
              :disabled="false"
              style="width: 350px"
            />
            <el-input
              v-model="field.model"
              :disabled="false"
              style="width: 350px"
            />
          </template>
          <template v-else-if="dialogFields.type === 'bindVideo'">
            <el-input
              v-model="field.model"
              :disabled="false"
              style="width: 350px"
            />
          </template>
          <template v-if="field.type === 'text'">
            <template v-for="(item, index) in field.textArr" :key="index">
              <el-tag class="tag" effect="plain">{{ item }}</el-tag>
            </template>
          </template>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">{{ translate('取消') }}</el-button>
          <el-button type="primary" @click="confirm">
            {{ translate('确定') }}
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import { translate } from '@/i18n'
  const props = defineProps({
    visible: {
      type: Boolean,
      default: false,
    },
    fields: {
      type: Array as any,
      default() {
        return []
      },
    },
    title: {
      type: String,
      default: translate('提示'),
    },
    cslist: {
      type: Array as any,
      default() {
        return []
      },
    },
    dialogtype: {
      type: String,
      default: '',
    },
  })
  const dialogVisible = ref(false)
  const dialogFields: any = ref(props.fields)
  const diologcslist: any = ref(props.cslist)
  watch(
    () => props.visible,
    () => {
      dialogVisible.value = props.visible
      if (props.visible) {
        dialogFields.value = [].concat(props.fields)
        diologcslist.value = [].concat(props.cslist)
        console.log(dialogFields.value)
        console.log(diologcslist.value)
      }
    }
  )
  const emit = defineEmits(['close', 'confirm'])
  const handleClose = () => {
    dialogVisible.value = false
    emit('close', dialogVisible.value)
  }
  const confirm = () => {
    emit('confirm', dialogFields.value)
    // dialogVisible.value = false
  }
</script>

<style lang="scss" scoped>
  .tag {
    font-size: 14px;
  }
</style>
